﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using ClassLibrary.DAO;
using ClassLibrary.Entity;
using System.IO;

public partial class Admin_UserControl_ucAddCategories : System.Web.UI.UserControl
{
    TblCategoriesDAO CateDAO = new TblCategoriesDAO(Global.CONNECTION_STRING);
    public static int index;
    private static string sortExpression = null;
    private static string direction = null;
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            this.ViewState["SelectedText"] = "All";
            load_Category();
            DetailCate();

        }
    }

    private void load_Category()
    {
        grdCate.DataSource = CateDAO.GetAll();
        grdCate.DataBind();
    }

    private void DetailCate()
    {
        string id = Request.QueryString["id"];
        if (id == null)
        {
           
        }
        else
        {

            TblCategories Category = CateDAO.GetCateByID(Global.ConvertToInt(id));
            txtCateName.Text = Category.CateName;
            txtCateDes.Text = Category.CateDescription;
            if (Category.IDGroup == 1)
            {
                drgroup.SelectedItem.Text = "iPhone-iPad-iPod";
            }
            if (Category.IDGroup == 2)
            {
                drgroup.SelectedItem.Text = "Macbook-Mac mini-iMac";
            }
            if (Category.IDGroup == 3)
            {
                drgroup.SelectedItem.Text = "Phụ kiện iPhone";
            }
            if (Category.IDGroup == 4)
            {
                drgroup.SelectedItem.Text = "Phụ kiện iPad";
            }
            if (Category.IDGroup == 5)
            {
                drgroup.SelectedItem.Text = "Phụ kiện iPod";
            }
        }
    }

    // kiem tra co trung ten khong truoc khi insert
    private bool Check_CateExists()
    {
        bool bExists = false;
        try
        {

            bExists = CateDAO.check_cate(txtCateName.Text);
            System.Threading.Thread.Sleep(3000);
        }
        catch (Exception ex)
        {
            Session[Global.SESSION_ERROR] = ex;
            Response.Redirect("~/Error.aspx", true);
        }
        return bExists;
    }
    protected void btAdd_Click(object sender, EventArgs e)
    {
        bool flag = false;
        try
        {
            TblCategories Cate = new TblCategories();
            string id = Request.QueryString["id"];

            Cate.CateName = txtCateName.Text;
            Cate.CateDescription = txtCateDes.Text;
            Cate.IDGroup =Convert.ToInt32(drgroup.SelectedValue);

            //string filename = upload.FileName;

            //string path = Server.MapPath("~\\Admin\\images\\items\\");

            if (id == null || id == "")
            {
                if (Check_CateExists())
                {
                    lb_message.Text = "Loại sản phẩm này đã có.";
                    return;
                }
                else
                {

                    //if (File.Exists(path + "\\" + filename))
                  //  {
                   //     lbmesage.Text = "Hình này đã có. Vui lòng chọn 1 file hình khác để upload. Xin cảm ơn !";
                   //     return;
                   // }
                    //upload.SaveAs(path + "\\" + filename);

                    Cate.ImageURL = "";

                    Cate.CateID = 0;
                    CateDAO.Insert(Cate, 1);
                }

            }
            else
            {
                //Cate.CateID = Global.ConvertToInt(id);
                int id1 = Global.ConvertToInt(Request.QueryString["id"]);
                TblCategories Category = CateDAO.GetCateByID(id1);
                if (Category != null)
                {
                   // string fname = Category.ImageURL;
                   // if (fname != null)
                   // {
                   //     File.Delete(Server.MapPath("~\\Admin\\images\\items\\" + fname));
                   // }
                    // Update moi
                    Cate.CateID = id1;
                    Cate.CateName = txtCateName.Text;
                    Cate.CateDescription = txtCateDes.Text;
                    Cate.IDGroup = Convert.ToInt32(drgroup.SelectedValue);
                    //Cate.ImageURL = filename;
                    Cate.ImageURL = "";
                    //upload.SaveAs(Server.MapPath("~\\Admin\\images\\items\\" + filename));
                    CateDAO.Insert(Cate, 2);
                }

            }
            flag = true;
        }
        catch (Exception ex)
        {
            Session[Global.SESSION_ERROR] = ex;
            Response.Redirect(Global.ERROR_PAGE_ADMIN);
        }
        if (flag)
        {
            load_Category();
            // Panel1.Visible = false;
        }
    }

    private void BindGrid(string StartAlpha)
    {
        DataTable dt_dept = CateDAO.GetAll();
        DataView dv = new DataView(dt_dept); // dt_dept là table ch?a d? li?u c?a gridview
        dv.Sort = sortExpression + direction;
        grdCate.DataSource = dv;
        grdCate.DataBind();

    }
    protected void grdCate_PageIndexChanging(object sender, GridViewPageEventArgs e)
    {
        grdCate.PageIndex = e.NewPageIndex;
        //LoadDriver();
        BindGrid(ViewState["SelectedText"].ToString());
    }
}
